import { Schema, model } from "mongoose";
import Types from "../typings";

// 定义 Schema 模型
const JobSchema = new Schema<Types.Job.JobType>({
  id: {
    type: String,
    required: true,
    unique: true,
  },

  title: {
    type: String,
    required: true,
    unique: true,
  },

  level: {
    type: String,
    required: true,
    default: "",
  },

  isFinish: {
    type: Boolean,
    required: true,
    default: false,
  },

  category: {
    type: String,
    required: true,
    default: "",
  },

  isTop: {
    type: Boolean,
    default: false,
  },

  content: {
    type: String,
    required: true,
  },

  projectCode: {
    type: String,
    default: "",
  },

  projectName: {
    type: String,
    default: "",
  },

  createdTime: {
    type: Number,
    Number,
    required: true,
    default: Date.now(),
  },

  updatedTime: {
    type: Number,
    required: true,
    default: Date.now(),
  },

  creatorId: {
    type: String,
    required: true,
    default: "",
  },
});

// 排除 _id 和 __v 字段
JobSchema.set("toObject", {
  virtuals: true,
  versionKey: false,
  transform: (doc, ret) => {
    delete ret._id;
    delete ret.__v;
  },
});

JobSchema.set("toJSON", {
  virtuals: true,
  versionKey: false,
  transform: (doc, ret) => {
    delete ret._id;
    delete ret.__v;
  },
});

const JobModel = model<Types.Job.JobType>("Job", JobSchema, "jobs");

export default JobModel;
